//  蓝牙设备列表界面
//  ViewController.m
//  blocklyios
//
//  Created by steward on 2018/11/4.
//  Copyright © 2018 szltech. All rights reserved.
//

#import "ViewController.h"
#import "BluetoothAdapter.h"
#import "UIUtils.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIImageView *backIcon;
@property BluetoothAdapter *adapter;
@property NSMutableArray<CBPeripheral *> *devices;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initBlueTooth];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.baby) {
        //设置委托后直接可以使用，无需等待CBCentralManagerStatePoweredOn状态。
        [self scanDevices];
    }
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (self.baby) {
        [self.baby cancelScan];
    }
}

- (IBAction)clickBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initView {
    self.adapter = [BluetoothAdapter.alloc initWithTableView: self.tableView];
    @weakify(self);
    self.adapter.listener = ^(NSInteger row) {
        @strongify(self);
        [self.baby cancelScan];
        self.mPeripheral = self.adapter.getItems[row];
        self.fetchDevice(self.mPeripheral);
        [self.navigationController popViewControllerAnimated:YES];
    };
    self.devices = [NSMutableArray.alloc init];
}

-(void)initBlueTooth {
    [super initBlueTooth];
    //设置蓝牙委托
    [self babyDelegate];
    if (self.baby.centralManager.state != CBCentralManagerStatePoweredOn) {
        [UIUtils showOneButtonDialog:self title:@"提示" content:@"您的蓝牙未开启，请打开蓝牙" btnStr:@"确定" clickEvent:nil];
    }
}

//设置蓝牙委托
-(void)babyDelegate {
    //设置扫描到设备的委托
    @weakify(self);
    [self.baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        @strongify(self);
        NSLog(@"搜索到了设备:%@",peripheral.name);
        if (peripheral && ![self.devices containsObject:peripheral] && peripheral.name.length > 0) {
            [self.devices addObject:peripheral];
            [self.adapter setItems:self.devices];
        }
        
    }];
    
    //过滤器
    //设置查找设备的过滤器
    [self.baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {
        @strongify(self);
        if ([self isDeviceValid:peripheralName]) {
            return YES;
        }
        return NO;
    }];
    [self.baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
        @strongify(self);
        if (central.state == CBCentralManagerStatePoweredOn) {
            if (self.baby) {
                [self scanDevices];
            }
        } else {
            if (self.baby) {
                [self.baby cancelScan];
            }
        }
    }];
}

- (BOOL)isDeviceValid: (NSString *)deviceName {
    return YES;
//    return (deviceName.length > 0) && ([deviceName hasPrefix:@"ET11"] || [deviceName hasPrefix:@"BLE"] || [deviceName hasPrefix:@"TQ"]);
}

@end
